import Constants from "../../utils/Constants";
import {message} from 'antd';
import {download} from "../../utils/commons";
import {exportTaxStatus, getTaxStatus} from "../../services/report/taxInv";
import {getCustomerByNameOrCode} from "../../services/customer/customer";
const R=require("ramda");
export default {
    namespace:"taxInvReport",
    state:{
        page:{},

    },
    reducers:{
        setTaxStatus(state,{payload})
        {
            return {...state,taxStatusList:payload.taxStatus,page:payload.page}
        },
        setCustomer(state, action) {
            let payload = action.payload;
            return {...state, cstList: payload.data};
        },
    },

    effects:{
        *getTaxStatus({payload},{call,put,select}){
            payload= R.isNil(payload)?null:payload;
            let page=R.isNil(payload)?Constants.DEFAULTPAGE:R.isNil(payload.page)?Constants.DEFAULTPAGE:payload.page;
            let beginTime=R.isNil(payload)?null:payload.beginTime;
            let endTime=R.isNil(payload)?null:payload.endTime;
            let cstId=R.isNil(payload)?null:payload.cstId;
            let orderNo=R.isNil(payload)?null:payload.orderNo;
            let declaratNo=R.isNil(payload)?null:payload.declaratNo;
            let drawbackFianceStatus=R.isNil(payload)?null:payload.drawbackFianceStatus;
            let presentStatus=R.isNil(payload)?null:payload.presentStatus;
            let transferAccountStatus=R.isNil(payload)?null:payload.transferAccountStatus;

            const res=yield call(getTaxStatus,{
                cstId:cstId,
                orderNo:orderNo,
                declaratNo:declaratNo,
                drawbackFianceStatus:drawbackFianceStatus,
                presentStatus:presentStatus,
                transferAccountStatus:transferAccountStatus,
                exportStart:beginTime,
                exportEnd:endTime,
                page});
            let content=res.data.content;
            let data=content.data.list;
            if(res.data.resultCode=="ok")
            {
                yield put({
                    type:'setTaxStatus',
                    payload:{
                        taxStatus:data,
                        page:R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                    }
                })
            }


        },
        *getCstBaseInfo({payload},{call,put,select}){
            payload= R.isNil(payload)?null:payload;
            const res= yield call(getCustomerByNameOrCode,{
                nameOrCode:payload.cstId
            })
            if (res.data.resultCode === "ok") {
                let data = res.data;
                yield put({
                    type: "setCustomer",
                    payload: {
                        data: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        *exportTaxStatus({payload},{call,put,select})
        {
            payload=R.isNil(payload)?null:payload;
            let beginTime=R.isNil(payload)?null:payload.beginTime;
            let endTime=R.isNil(payload)?null:payload.endTime;
            let cstId=R.isNil(payload)?null:payload.cstId;
            let orderNo=R.isNil(payload)?null:payload.orderNo;
            let declaratNo=R.isNil(payload)?null:payload.declaratNo;
            let drawbackFianceStatus=R.isNil(payload)?null:payload.drawbackFianceStatus;
            let presentStatus=R.isNil(payload)?null:payload.presentStatus;
            let transferAccountStatus=R.isNil(payload)?null:payload.transferAccountStatus;

            const res=yield call(exportTaxStatus,{
                orderNo:orderNo,
                declaratNo:declaratNo,
                drawbackFianceStatus:drawbackFianceStatus,
                presentStatus:presentStatus,
                transferAccountStatus:transferAccountStatus,
                exportStart:beginTime,
                exportEnd:endTime,
                cstId:cstId,
            });

        }
    },

    subscriptions:{
        setup({history,dispatch}){
            return history.listen(({pathname})=>{
                if(pathname.includes('report/taxinv'))
                {
                    dispatch({
                        type:"getTaxStatus",
                    })
                }
            });

        }
    }

}